library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.numeric_std.all;
use work.state_pkg.ALL;


entity whiteinterrupt is
	port ( clk 	:	in	std_logic;
		reset 	:	in	std_logic;
		
		sensor_l :	in	std_logic;
		sensor_m :	in	std_logic;
		sensor_r :	in	std_logic;
		max_count : in unsigned(31 downto 0);
		
		continu	:	out	std_logic;
		rover_direction	:	out	direction_type;
		dbm		: out	string(2 downto 1)
		);		
end whiteinterrupt;

architecture Behavioral of whiteinterrupt is

	type whiteinterrupt_state is (find, stop, found);
	
       signal state, new_state:  whiteinterrupt_state;
		 signal sensor: std_logic_vector(2 downto 0);
		 signal count : unsigned(31 downto 0);
		 
begin
    sensor <= sensor_l & sensor_m & sensor_r;
    
    process (clk)
        begin
               if (rising_edge (clk)) then
                   if (reset = '1') then
                        state <= find;
								count <= to_unsigned(0, 32);
						 elsif (count >= max_count) then
								state <= stop;
                   else
                        state <= new_state;
								count <= count +1;
                   end if;
               end if;
        end process;
    

    process (state, sensor)
        begin
          
               case state is
						 when find =>
								rover_direction <= straight;
							   dbm <= "FI";
								continu <= '1';
								if (sensor = "111") then
                              new_state <= find;
								else 
										new_state <= found;
								end if;
						
						 when found =>
								rover_direction <= straight;
								dbm <= "FO";
								continu <= '0';
								new_state <= found;
								
						 when stop =>
								rover_direction <= stop;
								dbm <= "St";
								continu <= '1';
								new_state <= stop;
																															
                end case;
            end process;

end Behavioral;

